<%@ page import="com.shop.shop_end.entity.Admin" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="ajax.jsp"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>"/>
    <meta charset="UTF-8">
    <title>欢迎访问后台管理系统</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .header {
            background-color: #1aa094;
            color: #fff;
            padding: 10px;
            text-align: center;
        }

        .header .logo {
            font-size: 22px;
            text-decoration: none;
            color: #fff;
        }

        .header .admin-side-toggle {
            float: right;
            cursor: pointer;
        }

        .admin-header-item {
            float: right;
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .admin-header-item li {
            display: inline-block;
            margin-right: 10px;
        }

        .admin-header-item a {
            color: #fff;
            text-decoration: none;
        }

        .admin-header-item a:hover {
            text-decoration: underline;
        }

        .footer {
            background-color: #1aa094;
            color: #fff;
            text-align: center;
            padding: 10px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }

        .sidebar {
            font-weight: bold;
            width: 200px;
            background-color: #333;
            color: #fff;
            position: fixed;
            top: 0px;
            bottom: 0;
            left: 0;
            padding-top: 70px;
        }

        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar li {
            padding: 10px;
            cursor: pointer;
        }

        .submenu {
            font-weight: normal;
            display: none;
            background-color: #444;
        }

        .submenu li {
            padding: 10px 20px;
            cursor: pointer;
        }

        .content {
            background-color: #fff;
            color: #000;
            position: fixed;
            top: 40px;
            bottom: 0;
            left: 200px;
            width: calc(100% - 200px);
            height: calc(100% - 180px);
            padding-top: 70px;
        }
        a{
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="header">
    <a class="logo" onclick="editUserInfo()">后台管理系统</a>
    <div class="admin-side-toggle">
        <i class="fa fa-bars" aria-hidden="true"></i>
    </div>
    <ul class="admin-header-item">
        <li><a onclick="editUserInfo()">${sessionScope.admin.username}</a></li>
        <li>
            <a onclick="exitLogin()">
               注销
            </a>
        </li>
    </ul>
</div>
<div class="sidebar">
    <ul>
        <li class="faMenu">个人信息管理</li>
        <ul class="submenu">
            <li onclick="editUserInfo()">个人信息编辑</li>
            <li onclick="loadPage('/admin/prePwd')">密码重置</li>
        </ul>
        <li onclick="loadPage('/admin/createAdmin')">增加管理员</li>
        <li onclick="loadPage('/user/getAllUsers')">网站用户信息管理</li>
        <li class="faMenu">商品信息管理</li>
        <ul class="submenu">
            <li onclick="loadPage('/good/getAllGoods')">商品信息列表</li>
            <li onclick="loadPage('/cate/getAllCates')">商品类型列表</li>
        </ul>
        <li class="faMenu">订单信息管理</li>
        <ul class="submenu">
            <li onclick="loadPage('/order/getAllOrders')">订单信息列表</li>
            <li onclick="loadPage('/topic/getAllTopics')">订单评价列表</li>
        </ul>
        <li onclick="loadPage('/complain/getAllComplains')">意见反馈</li>
    </ul>
</div>
<iframe class="content" onclick="editUserInfo()" src="" style="padding: 5px 0 0 0;"></iframe>
<div class="footer">
    <p>&copy; 后台管理系统 All Rights Reserved</p>
</div>
<script>
    window.onload = function () {
        editUserInfo();
    }

    function editUserInfo(){
        console.log("开始获取session");
        var url = '/public/setSession?token=' + encodeURIComponent(localStorage.getItem('token'));
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        if (userInfo == null || userInfo === 'undefined' || userInfo === ''){
            alert("未登录，请先登录");
            window.location.href = '/admin/loginPage';
            return false;
        }
        console.log("userInfo==>", userInfo);
        let adminId = userInfo.adminId;
        axiosInstance.get(url).then((res)=>{
            if (res.data.code == 200){
                console.log("session设置成功");
            }else {
                console.log("token过期或其他异常");
                alert("token过期或其他异常");
                window.location.href = '/admin/loginPage';
                return false;
            }
        }).catch((e)=>{
            console.log(e);
        })
        setTimeout(()=>{
            console.log("adminId==>", adminId);
            loadPage('/admin/getAdminById/?adminId='+adminId);
        }, 300);
        // reloadPage();
    }

    //自动刷新一次页面
    function reloadPage() {
        if (location.href.indexOf('#reloaded') == -1) { //判断是否有刷新标记
            location.href = location.href + "#reloaded";//没有添加标记
            location.reload();//刷新
        }
    }

    //注销登录
    function exitLogin(){
        let flag = confirm('确认退出?');
        if (flag === true){
            localStorage.removeItem('userInfo');
            localStorage.removeItem('token');
            localStorage.removeItem('role');
            location.href = '/admin/exit';
        }
    }

    let faMenu = document.getElementsByClassName('faMenu');
    for (let i = 0; i < faMenu.length; i++) {
        let index = i;
        faMenu[index].onclick = function () {
            console.log("111");
            var submenu = document.getElementsByClassName('submenu')[index];
            if (submenu.style.display === 'block') {
                submenu.style.display = 'none';
            } else {
                submenu.style.display = 'block';
            }
        };
    }

    function loadPage(pageUrl) {
        let token = localStorage.getItem('token');
        pageUrl += (pageUrl.indexOf("?") === -1 ? "?" : "&") + "token=" + encodeURIComponent(token);
        // $.ajaxSetup({
        //     headers: headersJson
        // })
        $.ajax({
            url: pageUrl,
            type: 'GET',
            success: function (data) {
                $('.content')[0].src = pageUrl
                // $('#content').html(data);
            },
            error: function () {
                $('.content')[0].html('加载页面失败');
            }
        });
    }

</script>
</body>
</html>
